#coding=utf8
import itchat,sys
from itchat.content import *

reload(sys)
sys.setdefaultencoding('utf-8')

@itchat.msg_register(itchat.content.TEXT,isGroupChat=True)
def print_content(msg):
	source = msg['FromUserName'] #消息来源ID
	GroupName = msg['User']['NickName'] #消息来源群名
	userName = msg['ActualNickName'] #用户名
	content = msg['Content'] #消息字符串
	keyword = ['John','赵成翰'] #关键词
	for i in keyword:
		if content.find(i) >= 0: #判断消息是否存在关键词
			print('群聊[%s]的[%s]说:↓\n%s' %(GroupName,userName,content)) #打印符合关键词的消息
			itchat.send('群聊[%s]的[%s]说:↓\n%s' %(GroupName,userName,content),'filehelper') #向文件传输助手发送消息
			itchat.send('群聊[%s]的[%s]说:↓\n%s' %(GroupName,userName,content),source) #原路返回消息
			break
itchat.auto_login(hotReload=True) #登录微信 #hotReload值为是否自动登录 #enableCmdQR值为是否命令行打印登录二维码
chatrooms = itchat.get_chatrooms(update=True, contactOnly=True)
chatroom_ids = [c['UserName'] for c in chatrooms]
# print str(chatrooms)
print u'正在监测的群聊：', len(chatrooms), '个'
print u' | '.join([item['NickName'] for item in chatrooms])
itchat.run()